package com.qf.purchasing.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.result.ResultCode;
import com.qf.purchasing.entity.Detail;
import com.qf.purchasing.entity.Schedule;
import com.qf.purchasing.mapper.DetailMapper;
import com.qf.purchasing.mapper.ScheduleMapper;
import com.qf.purchasing.qo.ScheduleQO;
import com.qf.purchasing.service.ScheduleService;
import com.qf.purchasing.vo.ScheduleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;


@Service
public class ScheduleServiceImpl implements ScheduleService {
    //flag[0] :增加修改标志  1：用户添加操作，2：用户删除操作
    //flag[1] :增加或修改的计划 id
    public static int[] flag = new int[2];


    @Resource
    private ScheduleMapper scheduleMapper;

    @Resource
    private DetailMapper detailMapper;


    @Override
    public PageInfo<ScheduleVO> query(int page, int size, String title, String year) {
        PageInfo<ScheduleVO> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(
                () -> scheduleMapper.selectSchedule(title, year)
        );
        if (pageInfo.getTotal() == 0) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
        return pageInfo;
    }


    @Override
    public RespResult<ScheduleVO> selectScheduleById(int scheduleId) {
        Schedule schedule = scheduleMapper.selectById(scheduleId);
        if (!ObjectUtils.isEmpty(schedule)) {
            ScheduleVO scheduleVO = new ScheduleVO();
            BeanUtils.copyProperties(schedule, scheduleVO);
            return RespResult.success(ResultCode.SUCCESS, scheduleVO);
        } else {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
    }

    @Override
    public RespResult<String> insert(ScheduleQO scheduleQO) {
        //查询此条明细是否存在，根据id判断
        Schedule scheduleResult = scheduleMapper.selectById(scheduleQO.getId());
        if (ObjectUtils.isEmpty(scheduleResult)) {
            //不存在，插入
            Schedule schedule = new Schedule();
            BeanUtils.copyProperties(scheduleQO, schedule);
            int count = scheduleMapper.insert(schedule);
            if (count < 1) {
                throw new ServiceException(ResultCode.INSERT_ERROR);
            }

            flag[0] = 1;
            flag[1] = scheduleQO.getId();
            return RespResult.success("添加成功！");
        } else {
            throw new ServiceException(ResultCode.INSERT_ERROR);
        }
    }

    @Override
    public RespResult<String> modify(ScheduleQO scheduleQO) {
        Schedule schedule = new Schedule();
        BeanUtils.copyProperties(scheduleQO, schedule);
        int count = scheduleMapper.updateById(schedule);
        if (count < 1) {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        }
        return RespResult.success("记录更新成功！");
    }


    @Transactional
    @Override
    public RespResult<String> deleteScheduleOne(int id) {
        int count = scheduleMapper.updateStatusById(id);
        if (count > 0) {
            //计划删除成功，把对应的采购明细也全部删除
            List<Detail> details = detailMapper.selectByScheduleId(id);
            if (!details.isEmpty()) {
                //如果计划存在明细，则删除所有明细
                count = detailMapper.updateStatusByScheduleId(id);
                if (count > 0) {

                    return RespResult.success("采购计划和对应明细均删除成功！");
                } else {
                    throw new ServiceException(ResultCode.DELETE_ERROR);
                }
            }

            flag[0] = 2;
            flag[1] = id;
            return RespResult.success("删除成功,无采购明细！");
        } else {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        }
    }


    @Override
    public RespResult<String> deleteSchedules(int[] ids) {
        try {
            for (int i = 0; i < ids.length; i++) {
                deleteScheduleOne(ids[i]);
            }
            return RespResult.success("删除成功");
        } catch (Exception e) {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        }
    }

    @Override
    public RespResult<String> recallSchedule() {
        if (flag[0] == 1) {
            int temp1 = flag[0];
            int temp2 = flag[1];
            //上一次操作是新增，则删除新增数据
            RespResult<String> result = deleteScheduleOne(flag[1]);
            if ("success".equals(result.getMsg())) {
                flag[0] = temp1;
                flag[1] = temp2;
                return RespResult.success("撤回新增成功！");
            } else {
                throw new ServiceException(ResultCode.RECALL_ERROR);
            }
        } else {
            //上一次操作是删除
            RespResult<String> result = updateScheduleOne(flag[1]);
            if ("success".equals(result.getMsg())) {
                return RespResult.success("撤回删除成功！");
            } else {
                throw new ServiceException(ResultCode.RECALL_ERROR);
            }
        }
    }

    @Transactional
    @Override
    public RespResult<String> updateScheduleOne(int id) {
        int count = scheduleMapper.updateById(id);
        if (count > 0) {
            List<Detail> details = detailMapper.selectByScheduleId(id);
            if (!details.isEmpty()) {
                //撤销删除成功，把对应的采购明细删除也全部撤销
                count = detailMapper.updateByScheduleId(id);
                if (count > 0) {
                    return RespResult.success("采购计划和对应明细均修改成功！");
                } else {
                    throw new ServiceException(ResultCode.RECALL_ERROR);
                }
            }
            return RespResult.success("修改成功,无采购明细！");
        } else {
            throw new ServiceException(ResultCode.RECALL_ERROR);
        }
    }

}
